package com.tz.farm_feeding.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tz.farm_feeding.domain.User;
import com.tz.farm_feeding.domain.UserExample;
import com.tz.farm_feeding.domain.status;
import com.tz.farm_feeding.mapper.UserMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/user")
public class UserController {

  @Autowired
  UserMapper usermapperimpl;

  @PostMapping(value = "/getAllUser")
  public String logout(HttpServletRequest request, HttpSession session) {
    // session.setAttribute("userbase", null);
    JSONObject result = new JSONObject();
    UserExample userExample = new UserExample();
    UserExample.Criteria criteria = userExample.createCriteria();
    criteria.andGroupidBetween(1, 4);

    JSONArray jsonArray = new JSONArray();

    List<User> users = usermapperimpl.selectByExample(userExample);
    for (User user : users) {
      jsonArray.add(user);
    }
    result.put("result", status.s_ok);
    result.put("data", jsonArray);
    return result.toJSONString();
  }

  @PostMapping(value = "/addUser")
  public String addUser(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();

    if (jsonparm == null) {
      result.put("result", status.s_error);
      return result.toJSONString();
    }
    User user = new User();
    user.setName(jsonparm.getString("name"));
    user.setPassword(jsonparm.getString("pass"));
    user.setGroupid(jsonparm.getIntValue("groupid"));
    user.setImgurl(jsonparm.getString("imgurl"));
    user.setInfo(jsonparm.getString("info"));
    user.setPhone(jsonparm.getString("phone"));
    usermapperimpl.insert(user);
    result.put("result", status.s_ok);
    return result.toJSONString();
  }

  @PostMapping(value = "/updateuser")
  public String updateuser(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();

    if (jsonparm == null) {
      result.put("result", status.s_error);
      return result.toJSONString();
    }
    User user = new User();
    int id = jsonparm.getInteger("id");
    user.setName(jsonparm.getString("name"));
    user.setId(id);
    user.setPassword(jsonparm.getString("pass"));
     user.setImgurl(jsonparm.getString("imgurl"));
    user.setInfo(jsonparm.getString("info"));
    user.setPhone(jsonparm.getString("phone"));
    user.setGroupid(jsonparm.getInteger("groupid"));

    // usermapperimpl.insert(user);
    result.put("result", status.s_ok);
    usermapperimpl.updateByPrimaryKeySelective(user);
    return result.toJSONString();
  }

  @PostMapping(value = "/delUser")
  public String delUser(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();

    if (jsonparm == null) {
      result.put("result", status.s_error);
      return result.toJSONString();
    }
    UserExample user = new UserExample();
    UserExample.Criteria criteria = user.createCriteria();
    criteria.andIdEqualTo(jsonparm.getInteger("id"));
    usermapperimpl.deleteByExample(user);
    result.put("result", status.s_ok);
    return result.toJSONString();
  }

}
